package com.lyf.controller.admin;

import com.aliyuncs.exceptions.ClientException;
import com.lyf.constant.MessageConstant;
import com.lyf.result.Result;
import com.lyf.utils.AliOssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@Api(tags = "通用接口")
@RequestMapping("/admin/common")
@RestController
public class CommonController {
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result upload(MultipartFile file){
        log.info("文件上传：{}", file.getOriginalFilename());
        String url = null;
        try {
            url = AliOssUtils.upload(file);
            log.info("文件上传成功：{}", url);
        }catch (IOException | ClientException e){
            log.error("文件上传失败：{}", e.getMessage());
            return Result.error(MessageConstant.UPLOAD_FAILED);
        }
        return Result.success(url);
    }
}
